#include "types.h"
#include "stat.h"
#include "user.h"
#include "fcntl.h"

#define BP 10

const uint rand[1000]={  654,918,234,835,113,912,648,795,68,518,968,425,975,399,759,107,732,187,849,995,553,452,92,127,491,431,106,99,803,94,
                        844,518,241,801,339,254,737,399,251,804,176,517,602,24,185,699,559,254,245,238,347,209,674,205,909,145,835,893,861,
                        640,374,185,409,708,468,633,172,402,312,92,226,16,448,195,565,403,556,955,268,542,125,329,822,42,987,992,928,611,380,
                        458,108,416,671,627,482,458,834,147,491,374,53,654,191,801,682,523,298,529,957,303,707,116,56,206,802,95,462,598,717,
                        990,378,680,246,490,52,118,127,889,503,691,670,117,259,552,938,355,843,810,138,806,986,499,814,540,769,470,550,301,
                        831,673,907,43,34,684,74,396,332,176,405,820,575,384,754,135,58,142,978,922,111,191,991,900,803,870,246,975,741,394,
                        179,717,27,283,916,209,84,998,451,183,202,844,600,919,823,665,206,978,57,965,65,956,57,152,703,699,105,995,203,482,
                        50,397,656,615,962,318,238,448,878,91,902,369,737,124,35,222,405,766,136,15,997,942,550,781,739,729,521,530,125,641,
                        143,304,181,979,856,353,60,355,45,53,679,776,478,933,30,237,874,978,406,592,228,857,85,430,795,847,246,753,228,420,
                        338,490,891,281,730,88,714,102,268,23,758,244,521,81,105,595,37,101,658,462,578,592,212,940,767,100,897,752,260,850,
                        798,991,176,551,412,683,10,106,28,442,641,1000,760,419,118,422,263,409,453,633,672,658,787,833,769,447,895,127,281,
                        400,355,397,858,922,813,267,183,361,44,334,712,327,313,974,952,659,57,872,886,55,940,997,89,694,409,923,524,40,757,
                        470,542,284,507,720,102,503,611,223,403,644,29,82,323,927,855,248,33,890,70,252,641,768,245,889,613,310,583,679,606,
                        929,547,364,974,832,626,784,211,883,738,331,403,147,194,772,763,601,255,79,695,251,235,358,34,382,988,6,564,351,339,
                        157,966,801,775,78,726,344,641,728,972,903,520,584,792,374,534,619,200,130,320,888,788,809,646,723,137,938,341,170,
                        497,792,681,882,963,167,853,545,581,185,961,496,452,487,872,898,867,785,893,699,326,427,727,578,606,613,718,446,494,
                        422,272,809,697,253,214,961,224,646,702,376,168,143,74,351,80,71,299,350,758,892,911,350,944,281,225,160,822,649,783,
                        865,934,304,372,608,964,996,614,778,656,21,258,538,861,725,554,103,837,7,985,193,790,996,411,328,154,184,6,673,885,
                        911,922,529,467,380,831,434,799,798,428,623,934,720,913,638,341,727,302,917,380,459,519,605,957,94,843,630,866,254,
                        14,935,642,744,777,569,783,92,528,177,943,691,50,132,737,466,355,6,261,468,470,938,632,981,311,800,587,526,297,899,
                        672,594,522,389,179,82,126,200,437,479,781,81,281,772,535,24,940,486,649,753,598,530,405,22,546,888,384,251,280,359,
                        359,428,246,73,524,251,778,661,344,282,152,560,773,286,121,928,32,69,430,74,210,812,873,163,534,928,130,262,444,70,
                        83,440,531,477,396,502,180,416,686,644,879,734,823,771,551,528,849,120,493,536,875,429,556,885,238,727,599,526,711,
                        265,925,684,143,147,57,871,906,713,234,648,159,395,460,837,82,704,578,466,154,816,313,260,463,521,738,971,476,664,
                        1000,433,682,66,638,605,922,768,992,128,223,970,396,363,603,483,420,71,714,988,21,976,571,843,434,88,572,603,469,283,
                        918,216,923,785,677,151,875,607,176,691,830,690,364,425,174,701,387,908,850,310,506,440,612,378,568,745,300,167,177,
                        122,999,370,66,269,379,947,391,502,494,548,852,25,352,302,518,401,332,128,196,69,323,414,204,949,215,907,302,186,310,
                        60,360,177,991,57,981,188,751,950,814,21,476,914,701,822,508,113,51,884,833,574,188,288,212,912,148,929,330,608,408,
                        484,88,718,406,581,4,836,637,99,188,573,734,330,331,636,946,745,913,642,372,281,257,534,62,367,210,210,76,599,15,554,
                        10,75,486,50,256,407,186,380,526,574,281,494,96,269,909,692,940,459,406,844,487,131,194,958,109,996,689,643,451,919,
                        404,997,154,572,785,232,674,689,343,172,354,460,967,358,846,692,461,493,43,70,568,265,299,792,689,831,360,959,943,537,
                        414,22,2,715,5,149,402,105,935,211,130,108,335,721,48,534,312,807,951,13,224,749,636,104,589,967,940,908,754,376,641,
                        602,783,659,214,455,315,140,540,875,541,250,277,824,483,961,161,263,223,998,965,229,42,559,939,389,280,104,220,37,902,
                        476,930,925,702,44,522,656,830,958,225,141,48,600,396,803,253,743
};

int main( int argc, char *argv[] )
{
	int i, fd;
	if ( argc <= 1 )
	{
		printf( 1, "no argc\n" );
		exit();
	}
	int filenum = atoi( argv[1] );
	if ( filenum > 1000 )
	{
		printf( 1, "to mach testfile\n" );
		exit();
	}

	printf( 1, "------test large file read------\n" );

	int	start, end;
	char	str[512];
	char	filename[5];

    int time=0;
    start = uptime();
	for ( i = 0; i < filenum; i++ )
	{
        int tmp=rand[i]%filenum;
        itoa(tmp,filename);
        if ( (fd = open( filename , O_RDONLY ) ) < 0 )
		{
			printf( 1, "test: cannot open %s\n", filename );
			exit();
		}
		for ( int ii = 0; ii < BP; ii++ )
			read( fd, str, 512 );
        close(fd);
	}
    end = uptime();
    time+=end-start;
	double a=(double)time/filenum;
    int a1=a*10000/10000;
    int a2=((int)(a*10000))%10000;
    //printf(1,"%d\n",(int)(a*10000));
	printf( 1, "write time total: %d, avg: %d.%d\n", time, a1, a2);

	exit();
}